import 'package:base/utils/log_util.dart';
import 'package:framework/data/datasources/home_local_repository.dart';
import 'package:framework/data/model/stat_bean.dart';
import 'package:framework/main.dart';
import 'package:get/get.dart';

class DomainVM extends BaseViewModel {
  HomeRepository? _repository;

  /// 数据统计区 响应式数据列表
  final RxList<StatBean> statList = <StatBean>[].obs;
  final isExpanded = true.obs;
  final statLength = 0.obs;

  // 快捷操作栏 响应式数据列表
  final RxList<String> quickBarList = <String>[].obs;

  @override
  Future<void> init() async {
    _repository = HomeLocalRepository();
    await getDataStat();
    await getQuickBar();
  }

  Future<void> getDataStat() async {
    try {
      final List<StatBean>? data = await _repository?.getDataStatLists();
      statList.assignAll(data!);
      statLength.value = statList.length;
      Log.d("DomainVM getDataStat() ${statList.length}");
    } catch (e) {
      throw DataFailure('DataStat failed: $e');
    }
  }

  /// 快捷操作栏
  Future<void> getQuickBar() async {
    try {
      final List<String>? data = await _repository?.getQuickBarLists();
      quickBarList.assignAll(data!);
      Log.i("DomainVM getQuickBar() ${quickBarList.length}");
    } catch (e) {
      throw DataFailure('QuickVm failed: $e');
    }
  }

  updateExpanded() {
    isExpanded.value = !isExpanded.value;
    if (isExpanded.value) {
      statLength.value = statList.length;
    } else {
      statLength.value = 4;
    }
  }
}
